package com.ydlclass.registry;

import com.ydlclass.core.URL;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author it楠老师
 * @createTime 2023-03-11
 */
public class AbstractRegistry implements Registry{

    protected final String servicePrefix = "/yrpc/";

    // zookeeper://127.0.0.1:2181 | nacos://127.0.0.1:2181
    public AbstractRegistry(URL url) {
        this.url = url;
    }

    protected RegistryType registryType = RegistryType.ZOOKEEPER;
    
    protected URL url;

    protected String username;

    protected String password;
    
    @Override
    public void register(String serviceName) {
        // 1、通用的注册流程
        if(serviceName == null || "".equals(serviceName)){
            throw new IllegalArgumentException("You cannot register a service with an empty name.");
        }
        // 2、子类的独立实现
        this.doRegister(servicePrefix + serviceName);
    }

    // 留给子类扩展
    public void doRegister(String serviceName) {
    }

    @Override
    public void unregister(String serviceName) {
        doUnregister(servicePrefix + serviceName);
    }

    public void doUnregister(String serviceName) {

    }

    @Override
    public List<InetSocketAddress> lookup(String serviceName) {
        return doLookup(servicePrefix + serviceName);
    }

    public List<InetSocketAddress> doLookup(String serviceName) {
        return null;
    }

}
